package player;

/**
 * Class for applying changes to brightness of the video
 */

import javax.media.Buffer;
import javax.media.Effect;
import javax.media.Format;
import javax.media.format.RGBFormat;

public class MyBrightness implements Effect {

	private Format[] inputFormats;
	private Format[] outputFormats;
	private int brightness = 0;

	//constructor with input- and outputformats
	public MyBrightness() {
		inputFormats = new Format[] { new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
		outputFormats = new Format[] { new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
	}

	/*
	 * @author Michael Kascha 0548800
	 * The Brightness Effect
	 */
	public int process(Buffer in, Buffer out) {
		out.copy(in, true);
		byte[] data = (byte[]) out.getData();

		for (int i = 2; i < data.length; i = i+3) {

			// to int
			int r = (int) data[i];
			int g = (int) data[i-1];
			int b = (int) data[i-2];

			if (r < 0) { r += 256; }
			r = (int) (r + 256 * brightness/100);

			if (g < 0) { g += 256; }
			g = (int) ( g + 256 * brightness/100);

			if (b < 0) { b += 256; }
			b = (int) ( b + 256 * brightness/100);

			// make sure the value is between 0 and 255
			if (r < 0) { r = 0; } else if (r > 255) { r = 255; };
			if (g < 0) { g = 0; } else if (g > 255) { g = 255; };
			if (b < 0) { b = 0; } else if (b > 255) { b = 255; };

			// back to byte
			data[i] = (byte) r;
			data[i-1] = (byte) g;
			data[i-2] = (byte) b;
		}
		out.setData(data);
		return BUFFER_PROCESSED_OK;
	}
	
	//returns supported inputformats
	public Format[] getSupportedInputFormats() {
		return this.inputFormats;
	}

	//returns supported outputformats
	public Format[] getSupportedOutputFormats(Format f) {
		return new Format[] { this.outputFormats[0].intersects(f) };
	}
	
	//set inputformat
	public Format setInputFormat(Format input) {
		return input;
	}

	//set outputformat
	public Format setOutputFormat(Format output) {
		return output;
	}

	//return name of the effect
	public String getName() {
		return "Brightness Effect";
	}

	public void open() {
	}

	public void close() {
	}

	public void reset() {
		close();
		open();
	}

	public Object getControl(String controlType) {
		return null;
	}

	public Object[] getControls() {
		return null;
	}

	/*
	 * @author Michael Kascha 0548800
	 * setter for the Brightness Value, should be between -50 and +50 (%)
	 */
	public void setBrightness(int brightnessValue) {
		brightness = brightnessValue;
	}
}